package tcdx.uap.service;


import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tcdx.uap.common.utils.Lutils;
import tcdx.uap.common.utils.MapUtils;
import tcdx.uap.common.utils.SqlUtil;
import tcdx.uap.controller.SystemController;
import tcdx.uap.mapper.BaseDBMapper;
import tcdx.uap.mapper.FlowableMapper;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 参数配置 服务层实现
 * 
 * @author ruoyi
 */
@Service
public class BaseDBService
{

    @Autowired
    BaseDBMapper baseDBMapper;
    @Autowired
    private SystemController systemController;

    public List<Map> selectMaxCol(String tn,String  col){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        List<Map> l = baseDBMapper.selectMaxByCauses(MapUtils.G(
                "tn", tn, "col", col,
                "obj_c1", null));
        return l;
    }

    public Integer selectMaxColEq(String tn,String col, String eqCol, Object eqVal){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        List<Map> l = baseDBMapper.selectMaxByCauses(MapUtils.G(
                "tn", tn, "col", col,
                "obj_c1", SqlUtil.eq(eqCol, eqVal)));
        if(l!=null && l.size()>0 && l.get(0)!=null) {
            System.out.println(l.get(0));
            return (Integer) l.get(0).get(col);
        }
        else
            return null;
    }

    public List<Map> selectEq(String tn,Map eqMap){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        List<Map> l = baseDBMapper.selectEq(MapUtils.G(
                "tn", tn,
                "equalMap", eqMap));
        return l;
    }

    public List<Map> selectEq(String tn,Map eqMap, Map sortMap){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        List<Map> l = baseDBMapper.selectEq(MapUtils.G(
                "tn", tn,
                "equalMap", eqMap,
                "sortMap", sortMap
        ));
        return l;
    }

    public List<Map> selectEq(String tn, List<String>selectCols, Map eqMap){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        List<Map> l = baseDBMapper.selectEq(MapUtils.G(
                "tn", tn,
                "equalMap", eqMap,
                "selectCols", selectCols));
        return l;
    }


    public List<Map> selectEq(String tn, List<String>selectCols,Map eqMap, Map sortMap){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        List<Map> l = baseDBMapper.selectEq(MapUtils.G(
                "tn", tn,
                "equalMap", eqMap,
                "selectCols", selectCols,
                "sortMap", sortMap));
        return l;
    }


    public List<Map> selectIn(String tn, String columnName,List vals){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        List<Map> l = baseDBMapper.selectIn(MapUtils.G(
                "tn", tn,
                "columnName", columnName,
                "vals", vals));
        return l;
    }


    public List<Map> selectIn(String tn, List<String> selectCols, String columnName,List vals){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        List<Map> l = baseDBMapper.selectIn(MapUtils.G(
                "tn", tn,
                "selectCols", selectCols,
                "columnName", columnName,
                "vals", vals));
        return l;
    }

    public List<Map> selectIn(String tn, boolean distinct, List<String> selectCols, String columnName,List vals){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        List<Map> l = baseDBMapper.selectIn(MapUtils.G(
                "tn", tn,
                "selectCols", selectCols,
                "columnName", columnName,
                "vals", vals));
        return l;
    }


    public int insertMap(String tn, Map insertMap){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        int rs = baseDBMapper.insertMap(MapUtils.G(
                "tn", tn,
                "insertMap", insertMap));
        return rs;
    }


    public int updateEq(String tn, Map updateMap, Map equalMap){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        int rs = baseDBMapper.updateEq(MapUtils.G(
                "tn", tn,
                "updateMap", updateMap,
                "equalMap", equalMap));
        return rs;
    }


    public int updateIn(String tn, Map updateMap, String columnName, List vals){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        int rs = baseDBMapper.updateIn(MapUtils.G(
                "tn", tn,
                "updateMap", updateMap,
                "columnName", columnName,
                "vals", vals));
        return rs;
    }



    public int deleteEq(String tn, Map equalMap){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        int rs = baseDBMapper.deleteEq(MapUtils.G(
                "tn", tn,
                "equalMap", equalMap));
        return rs;
    }

    public List<Map> selectByCauses(String tn, Map causes, Map sortMap){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        List<Map> l = baseDBMapper.selectByCauses(MapUtils.G(
                "tn", tn,
                "obj_c1", causes,
                "sortMap", sortMap
        ));
        return l;
    }

    public List<Map> selectByCauses(String tn, List selColumns, Map causes, Map sortMap){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        List<Map> l = baseDBMapper.selectByCauses(MapUtils.G(
                "tn", tn,
                "selColumns", selColumns,
                "obj_c1", causes,
                "sortMap", sortMap
        ));
        return l;
    }

    public List<Map> selectByCauses(String tn,boolean distinct, List<String> selColumns,  Map causes, Map sortMap){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        List<Map> l = baseDBMapper.selectByCauses(MapUtils.G(
                "tn", tn,
                "distinct", distinct?"true": null,
                "selColumns", selColumns,
                "obj_c1", causes,
                "sortMap", sortMap
        ));
        return l;
    }

    public int executeSql(String sql){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        int rs= baseDBMapper.executeSql(sql);
        return rs;
    }


    public List<Map> selectSql(String sql){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        return baseDBMapper.selectSql(Lutils.genMap("sql" ,sql));
    }

    public List<Map> selectSql(Map params){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        return baseDBMapper.selectSql(params);
    }

    public List<Map> selectSql(Integer defined_sql_id,Map map){
        List<Map> sqls = selectEq("v_defined_sql", Lutils.genMap("id", defined_sql_id));
        map.remove("defined_sql_id");
        map.put("sql", sqls.get(0).get("sql").toString());
        return selectSql(map);
    }

    public int insertByBatch(String tableName, List<Map> insertMapData){
        int count = 0;
        for(Map insertData:insertMapData){
            count += baseDBMapper.insertMap(MapUtils.G(
                    "tn", tableName,
                    "insertMap", insertData));
        }
        return count;
    }


    public List<Map> selectNewById(String table_name){
        //获取跟流程相关联的字段，选取关键字段存入流程，以免过多数据库插入，影响效率
        return baseDBMapper.selectNewById(Lutils.genMap("tn", table_name));
    }


}
